package com.zjs.redis;

import com.zjs.redis.redisson.RedissonServiceImpl;
import lombok.extern.slf4j.Slf4j;
import com.util.result.Message;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Description TODO
 * @Author zhu_jinsheng[zhu_js@suixingpay.com]
 * @Date 2020/1/4 18:55
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/redis")
public class RedisTestController {

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Resource
    RedisHashUtils redisHashUtils;

    @Resource
    RedisUtils redisUtils;

    @Resource
    private RedissonServiceImpl redissonService;

    @PostMapping("/setHashKey")
    public String setRedis(){
        Integer currentUserNum = 2;
        redisTemplate.opsForValue().set("currentUserNumOfExportTradeData", currentUserNum.toString());
        Map map = new HashMap();
        map.put("用户1", new Date().toString());
        map.put("用户2", new Date().toString());
        map.put("用户3", new Date().toString());
        redisTemplate.delete("recentUserOfExportTradeData");
        map.remove("用户2");
        redisTemplate.opsForHash().putAll("recentUserOfExportTradeData", map);
        map.put("用户3", "111");
        redisTemplate.opsForHash().putAll("recentUserOfExportTradeData", map);
        return "设置成功";
    }

    @PostMapping("/getHashKey")
    public String getRedis(){
        String currentUserNum = redisTemplate.opsForValue().get("currentUserNumOfExportTradeData");
//        if (currentUserNum == null || Integer.parseInt(currentUserNum) <= 3) {
//            System.out.println(currentUserNum);
//        }
        Map a = redisTemplate.opsForHash().entries("recentUserOfExportTradeData");
//        log.info("map{}",map);
        System.out.println(currentUserNum);
        System.out.println(a.size());
        return "获取值成功";
    }

    @PostMapping("/test")
    public String testRedis(){
        redisUtils.stringTypeOperation();
        return "操作成功";
    }

    /**
     * Redisson框架分布式锁测试
     * @return
     */
    @PostMapping("/redissonRock")
    public Message redissonTest() {
        return redissonService.redissonRock();
    }
}
